package org.smile.util;

import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
/**
 * 对象长度获取工具类
 * @author strive
 *
 */
public class ObjectLenUtils {
	/**
	 * 是不是一样有长度的对象
	 * @param o 对象
	 * @return 整型长度
	 */
	public static  boolean hashLength(Object o){
		if(o==null){
			throw new NullPointerException("对象为空无法判断其长度");
		}else if(o instanceof Collection){
			return true;
		}else if(o instanceof Object[]){
			return true;
		}else if(o instanceof boolean[]){
			return true;
		}else if(o instanceof int[]){
			return true;
		}else if(o instanceof long[]){
			return true;
		}else if(o instanceof short[]){
			return true;
		}else if(o instanceof double[]){
			return true;
		}else if(o instanceof float[]){
			return true;
		}else if(o instanceof char[]){
			return true;
		}else if(o instanceof byte[]){
			return true;
		}
		return false;
	}
	/**
	 * 得到参数的对应索引的值
	 * @param o
	 * @param index
	 * @return
	 * @throws SQLException
	 */
	public static Object get(Object o,int index){
		if(o==null){
			throw new NullPointerException("对象为空无法判断其长度");
		}else if(o instanceof List){
			return ((List)o).get(index);
		}else if(o instanceof Collection){
			return ((Collection)o).toArray()[index];
		}else if(o instanceof Object[]){
			return ((Object[])o)[index];
		}else if(o instanceof boolean[]){
			return ((boolean[])o)[index];
		}else if(o instanceof int[]){
			return ((int[])o)[index];
		}else if(o instanceof long[]){
			return ((long[])o)[index];
		}else if(o instanceof short[]){
			return ((short[])o)[index];
		}else if(o instanceof double[]){
			return ((double[])o)[index];
		}else if(o instanceof float[]){
			return ((float[])o)[index];
		}else if(o instanceof char[]){
			return ((char[])o)[index];
		}else if(o instanceof byte[]){
			return ((byte[])o)[index];
		}else{
			return null;
		}
	}
	public static int len(Object o){
		if(o==null){
			throw new NullPointerException();
		}else if(o instanceof Collection){
			return ((Collection)o).size();
		}else if(o instanceof Object[]){
			return ((Object[])o).length;
		}else if(o instanceof boolean[]){
			return ((boolean[])o).length;
		}else if(o instanceof int[]){
			return ((int[])o).length;
		}else if(o instanceof long[]){
			return ((long[])o).length;
		}else if(o instanceof short[]){
			return ((short[])o).length;
		}else if(o instanceof double[]){
			return ((double[])o).length;
		}else if(o instanceof float[]){
			return ((float[])o).length;
		}else if(o instanceof char[]){
			return ((char[])o).length;
		}else if(o instanceof byte[]){
			return ((byte[])o).length;
		}
		return 1;
	}
}
